﻿
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using WinFormsApp1.Leo;

namespace WinFormsApp1.SuYi.week1WinForm
{
    public partial class BusinesWinForm : Form
    {
        public BusinesWinForm()
        {
            InitializeComponent();
            //comboBoxIndustrytag.Name
            GetRegion();
            Getindustrytag();
        }

        public record Provibces();

        /// <summary>
        /// 省
        /// </summary>
        private async void GetRegion()
        {
            Provibces provibces = new();

            var items = await HttpClientHelper.RequestUrl<Provibces, ApiResult<List<Provibcessend>>>(URLCommon.Url2, "/Week01Get/ProvibcesShow", HttpClientHelper.MethodType.POST, provibces);

            comboBoxProvibce.DataSource = items.Data;
            comboBoxProvibce.DisplayMember = "ProvibceName";
            comboBoxProvibce.ValueMember = "ProvibceId";

            this.Controls.Add(comboBoxProvibce);

        }
        /// <summary>
        /// 行业标签
        /// </summary>
        public record Industrytags();
        private async void Getindustrytag()
        {
            Industrytags industrytags = new();

            var items = await HttpClientHelper.RequestUrl<Industrytags, ApiResult<List<Industrytagdd>>>(URLCommon.Url2, "/WeekListOne_Leo/GetIndustrytags", HttpClientHelper.MethodType.POST, industrytags);

            comboBoxIndustrytag.DataSource = items.Data;
            comboBoxIndustrytag.DisplayMember = "IndustrytagName";
            comboBoxIndustrytag.ValueMember = "IndustrytagId";

            this.Controls.Add(comboBoxIndustrytag);

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private async void button1_Click(object sender, EventArgs e)
        {

            var businessName = textBoxName.Text;
            var businessAbbreviation = textBoxAbbreviation.Text;
            var businessImage = pictureBoxImage.Image;
            var industrytagId = Convert.ToInt32(comboBoxIndustrytag.SelectedValue);
            var provibceId = Convert.ToInt32(comboBoxProvibce.SelectedValue);
            var cityId = Convert.ToInt32(comboBoxCity.SelectedValue);
            var areaId = Convert.ToInt32(comboBoxArea.SelectedValue);
            var detailedAddress = textBoxAddress.Text;
            var businessPhone = textBoxPhone.Text;
            var businessDuty = textBoxDuty.Text;


            //var lable = new
            //{

            //};

            Business busine = new Business()
            {
                BusinessName = businessName,
                BusinessAbbreviation = businessAbbreviation,
                BusinessImage = businessImage.ToString(),
                IndustrytagId = industrytagId,
                ProvibceId = provibceId,
                CityId = cityId,
                AreaId = areaId,
                DetailedAddress = detailedAddress,
                BusinessPhone = businessPhone,
                BusinessDuty = businessDuty,
                IsDelete = false
            };
            var res = await HttpClientHelper.RequestUrl<Business, ApiResult<Business>>(URLCommon.Url1, "/Week1Manage/AddBusub", HttpClientHelper.MethodType.POST, busine);
            if (res.Code == 1)
            {
                MessageBox.Show("添加成功");
                this.Invalidate();
                this.Refresh();
            }
        }

        /// <summary>
        /// 图片点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBoxImage_Click(object sender, EventArgs e)
        {

            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "图片文件(*.jpg;*.jpeg;*.gif;*.bmp;*.png)|*.jpg;*.jpeg;*.gif;*.bmp;*.png";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        ///新的图片路径
                        string imagePath = openFileDialog.FileName;
                        //并显示出来
                        pictureBoxImage.Image = Image.FromFile(imagePath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("上传出错: " + ex.Message);
                    }
                }
            }


        }

        private void comboBoxIndustrytag_SelectedIndexChanged(object sender, EventArgs e)
        {

        }



        public record Citys(int provibceId);
        private async void comboBoxProvibce_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public record Areas(int cityId);
        private async void comboBoxCity_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 省点击事件
        /// </summary>
        /// <param name="provibceId"></param>
        private async void comboBoxProvibce_Cnlick(object sender, EventArgs e)
        {
            var provibceId = Convert.ToInt32(comboBoxProvibce.SelectedValue);

            Citys citys = new(provibceId);

            var items1 = await HttpClientHelper.RequestUrl<Citys, ApiResult<List<CitysTes>>>(URLCommon.Url2, "/WeekListOne_Leo/GetCitys", HttpClientHelper.MethodType.POST, citys);

            comboBoxCity.DataSource = items1.Data;
            comboBoxCity.DisplayMember = "CitysName";
            comboBoxCity.ValueMember = "CityId";

            this.Controls.Add(comboBoxCity);
        }
        /// <summary>
        /// 市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void comboBoxCity_Click(object sender, EventArgs e)
        {
            var cityId = Convert.ToInt32(comboBoxCity.SelectedValue);

            Areas areas = new(cityId);

            var items2 = await HttpClientHelper.RequestUrl<Areas, ApiResult<List<Areastes>>>(URLCommon.Url2, "/WeekListOne_Leo/GetAreas", HttpClientHelper.MethodType.POST, areas);

            comboBoxArea.DataSource = items2.Data;
            comboBoxArea.DisplayMember = "AreaName";
            comboBoxArea.ValueMember = "AreaId";


            this.Controls.Add(comboBoxArea);
        }

        private void comboBoxIndustrytag_Click(object sender, EventArgs e)
        {

        }

        public class Business
        {
            /// <summary>
            /// 企业编号
            /// </summary>
            public int BusinessId { get; set; }
            /// <summary>
            /// 企业名称
            /// </summary>
            public string BusinessName { get; set; }
            /// <summary>
            /// 企业简称
            /// </summary>
            public string BusinessAbbreviation { get; set; }
            /// <summary>
            /// 企业LOGO
            /// </summary>
            public string BusinessImage { get; set; }
            /// <summary>
            /// 行业标签(外键)
            /// </summary>
            public int IndustrytagId { get; set; }
            /// <summary>
            /// 省(外键)
            /// </summary>
            public int ProvibceId { get; set; }
            /// <summary>
            /// 市区(外键)
            /// </summary>
            public int CityId { get; set; }
            /// <summary>
            /// 区(外键)
            /// </summary>
            public int AreaId { get; set; }
            /// <summary>
            /// 详情地址
            /// </summary>
            public string DetailedAddress { get; set; }
            /// <summary>
            /// 企业电话
            /// </summary>
            public string BusinessPhone { get; set; }
            /// <summary>
            /// 负责人
            /// </summary>
            public string BusinessDuty { get; set; }
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsDelete { get; set; }
        }

        private void BusinesWinForm_Load(object sender, EventArgs e)
        {

        }
    }
}
